//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "PoissonDistribution.h"
using namespace Cephei::QL::Math::Distributions;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Distributions::CPoissonDistribution::CPoissonDistribution (Double mu) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phPoissonDistribution = NULL;
#endif
        QuantLib::Real _mu = (QuantLib::Real)ValueHelper::Convert (mu); //d
        _ppPoissonDistribution = new boost::shared_ptr<QuantLib::PoissonDistribution> (new QuantLib::PoissonDistribution ( _mu ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Distributions::CPoissonDistribution::CPoissonDistribution (boost::shared_ptr<QuantLib::PoissonDistribution>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPoissonDistribution = NULL;
#endif
	_ppPoissonDistribution = &childNative;
    
}
Cephei::QL::Math::Distributions::CPoissonDistribution::CPoissonDistribution (QuantLib::PoissonDistribution& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phPoissonDistribution = NULL;
#endif
	_ppPoissonDistribution = new boost::shared_ptr<QuantLib::PoissonDistribution> (&childNative);
    
    _PoissonDistributionOwner = owner;
    
}

Cephei::QL::Math::Distributions::CPoissonDistribution::CPoissonDistribution (CPoissonDistribution^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPoissonDistribution = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppPoissonDistribution = new boost::shared_ptr<QuantLib::PoissonDistribution> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Distributions::CPoissonDistribution::CPoissonDistribution (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPoissonDistribution = NULL;
#endif
	if (!t->IsSubclassOf(CPoissonDistribution::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Distributions::CPoissonDistribution::CPoissonDistribution (QuantLib::Handle<QuantLib::PoissonDistribution>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phPoissonDistribution = &childNative;
	_ppPoissonDistribution = &static_cast<boost::shared_ptr<QuantLib::PoissonDistribution>>(childNative.currentLink());
    
    _PoissonDistributionOwner = owner;
}
Cephei::QL::Math::Distributions::CPoissonDistribution::CPoissonDistribution (QuantLib::Handle<QuantLib::PoissonDistribution> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phPoissonDistribution = &childNative;
	_ppPoissonDistribution = &static_cast<boost::shared_ptr<QuantLib::PoissonDistribution>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Distributions::CPoissonDistribution::CPoissonDistribution (QuantLib::PoissonDistribution childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPoissonDistribution = NULL;
#endif
	_ppPoissonDistribution = new boost::shared_ptr<QuantLib::PoissonDistribution> (new QuantLib::PoissonDistribution (childNative));
    
}
#endif

Cephei::QL::Math::Distributions::CPoissonDistribution::~CPoissonDistribution ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppPoissonDistribution != NULL)
    {
	    delete _ppPoissonDistribution;
        _ppPoissonDistribution = NULL;
    }
}
Cephei::QL::Math::Distributions::CPoissonDistribution::!CPoissonDistribution ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppPoissonDistribution != NULL)
    {
	    delete _ppPoissonDistribution;
    }
}
QuantLib::PoissonDistribution& Cephei::QL::Math::Distributions::CPoissonDistribution::GetReference ()
{
    if (_ppPoissonDistribution == NULL) throw REFNEW NativeNullException ();
	return **_ppPoissonDistribution;
}
boost::shared_ptr<QuantLib::PoissonDistribution>& Cephei::QL::Math::Distributions::CPoissonDistribution::GetShared ()
{
    if (_ppPoissonDistribution == NULL) throw REFNEW NativeNullException ();
	return *_ppPoissonDistribution;
}
QuantLib::PoissonDistribution* Cephei::QL::Math::Distributions::CPoissonDistribution::GetPointer ()
{
    if (_ppPoissonDistribution == NULL) throw REFNEW NativeNullException ();
	return &**_ppPoissonDistribution;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::PoissonDistribution>& Cephei::QL::Math::Distributions::CPoissonDistribution::GetHandle ()
{
	if (_phPoissonDistribution == NULL)
	{
		_phPoissonDistribution = new Handle<QuantLib::PoissonDistribution> (*_ppPoissonDistribution);
	}
	return *_phPoissonDistribution;
}
#endif
bool Cephei::QL::Math::Distributions::CPoissonDistribution::HasNative () 
{
	return (_ppPoissonDistribution != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Distributions::IPoissonDistribution^ Cephei::QL::Math::Distributions::CPoissonDistribution_Factory::Create (Double mu)
{
    return REFNEW CPoissonDistribution ( mu);
}
